package com.voice.server.converter;

import com.voice.server.entity.Device;
import com.voice.server.enums.DeviceStatusEnum;
import com.voice.server.enums.PushTypeEnum;
import com.voice.server.output.DeviceInfo;
import com.voice.server.repository.UserRepository;
import com.wangjiacai.utils.converter.BasicConverter;
import com.wangjiacai.utils.utils.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

@Component
public class DeviceConverter extends BasicConverter<Device, DeviceInfo> {

    @Autowired
    private UserRepository userRepository;

    @Override
    public DeviceInfo newInstance() {
        return new DeviceInfo();
    }

    @Override
    public void preProcess(Device device, DeviceInfo deviceInfo) {
        super.preProcess(device, deviceInfo);
        deviceInfo.setStatus(EnumUtil.getMessageByCode(device.getStatus(), DeviceStatusEnum.class));
        deviceInfo.setPushType(EnumUtil.getMessageByCode(device.getPushType(), PushTypeEnum.class));
    }

    @Override
    public int getPropSize() {
        return 1;
    }

    @Override
    public Object getQueryParam(Device device, int i) {
        return device.getUserId();
    }

    @Override
    public String getQueryValueIdProp(int i) {
        return "id";
    }

    @Override
    public String getQueryValueProp(int i) {
        return "phone";
    }

    @Override
    public void setPropertyValue(DeviceInfo deviceInfo, int i, Object value) {
        deviceInfo.setPhone((String) value);
    }



    @Override
    public List queryList(int i, Object param, Object[] preMethodParam, Object[] postMethodParam) {
        return userRepository.findByIdIn((Collection<String>) param);
    }

    @Override
    public Object query(int i, Object param, Object[] preMethodParam, Object[] postMethodParam) {
        return userRepository.getOne((String) param);
    }
}
